﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BL;

namespace QuanLyCHTrangSuc
{
    public partial class ThongKe : Form
    {
        List<ThongKeDAL> dsThongKe = new List<ThongKeDAL>();
        List<ThongKeDAL> dsChitietThongKe = new List<ThongKeDAL>();
        List<ThongKeDAL> ThongKeTheoDieuKien = new List<ThongKeDAL>();
        List<ThongKeDAL> ThongKeTheoMaTK = new List<ThongKeDAL>();
        List<ThongKeDAL> Tim_NgayThangNam = new List<ThongKeDAL>();
        ThongKeDAL tk = new ThongKeDAL();
        public ThongKe()
        {
            InitializeComponent();
        }

        private void ThongKe_Load(object sender, EventArgs e)
        {
            LayDsThongKe();
            LayDsChitietThongKe();
        }
        protected void LayDsThongKe()
        {
            dsThongKe = ThongKeBL.LayDsThongKe();
            if (dsThongKe.Count == 0)
            {
                MessageBox.Show(" Không Tìm Thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            for (int i = 0; i < dsThongKe.Count; ++i)
            {

                ListViewItem lvi = new ListViewItem();
                lvi.Text = dsThongKe[i].MaThongKe;
                lvi.SubItems.Add(dsThongKe[i].NgayThongKe.ToString());
                lvi.Tag = (object)dsThongKe[i].MaThongKe;
                lv_ThongKe.Items.Add(lvi);
                
            }
        }

        private void LayDsChitietThongKe()
        {
            dsChitietThongKe = ThongKeBL.LayDsChitietThongKe();
            if (dsChitietThongKe.Count == 0)
            {
                MessageBox.Show(" Không Tìm Thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            for (int i = 0; i < dsChitietThongKe.Count; ++i)
            {

                ListViewItem lvi = new ListViewItem();
                lvi.Text = dsChitietThongKe[i].MaThongKe;
                lvi.SubItems.Add(dsChitietThongKe[i].MaDonDatHang.ToString());
                lvi.SubItems.Add(dsChitietThongKe[i].TongThu.ToString());
                lvi.SubItems.Add(dsChitietThongKe[i].TongChi.ToString());
                lvi.SubItems.Add(dsChitietThongKe[i].LoiNhuan.ToString());
                lvi.SubItems.Add(dsChitietThongKe[i].NgayThongKe.ToString());
                lvi.Tag = (object)dsChitietThongKe[i].MaThongKe;
                lvChiTietThongKe.Items.Add(lvi);
            }

        }

        private void btThongKe_Click(object sender, EventArgs e)
        {
            if (txtMaTKBC.Text == "" && txtMaDonDatHang.Text == "" && cbLoiNhuan.Text == "")
            {
                MessageBox.Show(" Nhập các thông tin cần tìm!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            lvChiTietThongKe.Items.Clear();
            ThongKeTheoDieuKien = ThongKeBL.ThongKeTheoDK(txtMaTKBC.Text, txtMaDonDatHang.Text, cbLoiNhuan.Text);
            if (ThongKeTheoDieuKien.Count == 0)
            {
                MessageBox.Show(" Không Tìm Thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            for (int i = 0; i < ThongKeTheoDieuKien.Count; ++i)
            {

                ListViewItem lvi = new ListViewItem();
                lvi.Text = ThongKeTheoDieuKien[i].MaThongKe;
                lvi.SubItems.Add(ThongKeTheoDieuKien[i].MaDonDatHang.ToString());
                lvi.SubItems.Add(ThongKeTheoDieuKien[i].TongThu.ToString());
                lvi.SubItems.Add(ThongKeTheoDieuKien[i].TongChi.ToString());
                lvi.SubItems.Add(ThongKeTheoDieuKien[i].LoiNhuan.ToString());
                lvi.SubItems.Add(ThongKeTheoDieuKien[i].NgayThongKe.ToString());
                lvi.Tag = (object)ThongKeTheoDieuKien[i].MaThongKe;
                lvChiTietThongKe.Items.Add(lvi);
            }
        }

        private void lv_ThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {
            lvChiTietThongKe.Items.Clear();
            if (lv_ThongKe.SelectedItems.Count > 0)
            {
                string maTK = this.lv_ThongKe.SelectedItems[0].Text;
                ThongKeTheoMaTK = ThongKeBL.tim_maTK(maTK);
                for (int i = 0; i < ThongKeTheoMaTK.Count; ++i)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = ThongKeTheoMaTK[i].MaThongKe;
                    lvi.SubItems.Add(ThongKeTheoMaTK[i].MaDonDatHang.ToString());
                    lvi.SubItems.Add(ThongKeTheoMaTK[i].TongThu.ToString());
                    lvi.SubItems.Add(ThongKeTheoMaTK[i].TongChi.ToString());
                    lvi.SubItems.Add(ThongKeTheoMaTK[i].LoiNhuan.ToString());
                    lvi.SubItems.Add(ThongKeTheoMaTK[i].NgayThongKe.ToString());
                    lvi.Tag = (object)ThongKeTheoMaTK[i].MaThongKe;
                    lvChiTietThongKe.Items.Add(lvi);
                }
            }
        }

        private void btTim_Click(object sender, EventArgs e)
        {
            lv_ThongKe.Items.Clear();
            Tim_NgayThangNam = ThongKeBL.tim_NgayThangNam(cbNgay.Text, cbThang.Text, txtNam.Text, txtmatThongKe.Text);
            if (Tim_NgayThangNam.Count == 0)
            {
                MessageBox.Show(" Không Tìm Thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            for (int i = 0; i < Tim_NgayThangNam.Count; ++i)
            {

                ListViewItem lvi = new ListViewItem();
                lvi.Text = Tim_NgayThangNam[i].MaThongKe;
                lvi.SubItems.Add(Tim_NgayThangNam[i].NgayThongKe.ToString());
                lv_ThongKe.Items.Add(lvi);
            }
        }

        private void btXemTK_Click(object sender, EventArgs e)
        {
            lv_ThongKe.Items.Clear();
            LayDsThongKe();
        }

        private void btXemChitiet_Click(object sender, EventArgs e)
        {
            lvChiTietThongKe.Items.Clear();
            LayDsChitietThongKe();
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            if (txtNam.Text != "")
            {
                try
                {
                    int u = Int32.Parse(txtNam.Text.Trim());
                }
                catch
                {
                    MessageBox.Show(" Không được nhập chữ cái! Nhập lại!!! ", "Chú Ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtNam.Text = "";
                    txtNam.Focus();
                    return;
                }
            } 
        }
    }
}
